package main;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author WIN7
 */
public class Character {
    private final int UP = 0;
    private final int DOWN = 1;
    private final int LEFT = 3;
    private final int RIGHT = 4;
    private Sprite sprite;
    private int[] seqMoveLeftFrame = {3, 4, 5};
    private int[] seqMoveRightFrame =  {9, 10, 11};
    private int[] seqMoveUpFrame = {6, 7, 8};
    private int[] seqMoveDownFrame = {0, 1, 2};
    private int currentDirection;
    private Pokemon ownPokemon;
    public Character(String filename, int widthFrame, int heighFrame , Pokemon ownPokemon)
    {
        try
        {
            sprite = new Sprite(Image.createImage(filename), widthFrame, heighFrame);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
            return;
        }
        this.ownPokemon = ownPokemon;
        currentDirection = DOWN;
        sprite.setFrameSequence(seqMoveDownFrame);
        sprite.defineCollisionRectangle(5, 5, widthFrame - 5, heighFrame - 5);
    }
    
    public void moveLeft()
    {
        if(currentDirection != LEFT)
        {
            sprite.setFrameSequence(seqMoveLeftFrame);
            currentDirection = LEFT;
        }else
        {
            //sprite.move(-2, 0);
        }
    }
    
    public void moveRight()
    {
        if(currentDirection != RIGHT)
        {
            sprite.setFrameSequence(seqMoveRightFrame);
            currentDirection = RIGHT;
        }else
        {
            //sprite.move(2, 0);
        }
    }
    
    public void moveUp()
    {
        if(currentDirection != UP)
        {
            sprite.setFrameSequence(seqMoveUpFrame);
            currentDirection = UP;
        }else
        {
            sprite.move(0, -2);
        }
    }
    
    public void moveDown()
    {
        if(currentDirection != DOWN)
        {
            sprite.setFrameSequence(seqMoveDownFrame);
            currentDirection = DOWN;
        }else
        {
            //sprite.move(0, 2);
        }
    }
    
    public void animation()
    {
        sprite.nextFrame();
        ownPokemon.getSprite().nextFrame();
    }
    
    public Sprite getSprite()
    {
        return sprite;
    }
    
    public void setPosition(int x, int y)
    {
        sprite.setPosition(x, y);
    }
    
    public void move(int dx, int dy)
    {
        sprite.move(dx, dy);
    }
    
    public int getX()
    {
        return sprite.getX();
    }
    
    public int getY()
    {
        return sprite.getY();
    }
    
    public boolean arrive(Item city)
    {
        return sprite.collidesWith(city.getSprite(), false);
    }
    
    public Pokemon getOwnPokemon()
    {
        return ownPokemon;
    }
}
